package com.springboot.blog.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.blog.beans.SignIn;
import com.springboot.blog.beans.SignInDto;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.SignInMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
public class SignServiceimpl {

    @Resource
    private SignInMapper signInMapper;
    public R signIn(String  userName) {
        SignIn siginCount = signInMapper.siginCount(userName);
        // 查询用户是否签过到
        QueryWrapper<SignIn> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SignIn::getUserName, userName);
        SignIn signIn = signInMapper.selectOne(queryWrapper);
        /*没有签过到, 直接新增*/
        if (null == signIn) {
            signInMapper.insert(new SignIn(userName,1,1,LocalDateTime.now()));
        } else {/*签过到*/
            // 判断最后签到日期与当前日期是否超过一天
            LocalDate signInTime = signIn.getUpdateTimes().toLocalDate();
            LocalDate currTime = LocalDate.now();
            long daysDiff = ChronoUnit.DAYS.between(signInTime, currTime);
            if (daysDiff <= 0) {
                return R.error().message("请勿重复签到");
            }
            if (daysDiff > 1) {
                // 1, 超过一天, 把连续签到的天数重置为 1
                signIn.setContinueDays(1);
                signIn.setReward(signIn.getReward()+1);

            } else {
                if (signIn.getContinueDays() == 7){
                    signIn.setContinueDays(signIn.getContinueDays() + 1);
                    signIn.setReward(signIn.getReward()+5);
                }else if (signIn.getContinueDays() == 14){
                    signIn.setContinueDays(signIn.getContinueDays() + 1);
                    signIn.setReward(signIn.getReward()+10);
                }else if (signIn.getContinueDays() == 21){
                    signIn.setContinueDays(signIn.getContinueDays() + 1);
                    signIn.setReward(signIn.getReward()+15);
                }else if (signIn.getContinueDays() == 28){
                    signIn.setContinueDays(signIn.getContinueDays() + 1);
                    signIn.setReward(signIn.getReward()+20);
                }else if (signIn.getContinueDays() > 28){
                    signIn.setContinueDays(signIn.getContinueDays() + 1);
                    signIn.setReward(signIn.getReward()+3);
                } else {
                    // 2, 没有超过一天, 把连续签到的天数+1
                    signIn.setContinueDays(signIn.getContinueDays() + 1);
                    signIn.setReward(signIn.getReward()+1);
                }

            }

            signIn.setUpdateTimes(LocalDateTime.now());
            signInMapper.updateById(signIn);
        }
        return R.ok();
    }

    /**
     * 用户签到列表
     */
    public R signInList(String  userName) {
        QueryWrapper<SignIn> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SignIn::getUserName, userName);
        SignIn signIn = signInMapper.selectOne(queryWrapper);
        List<SignInDto> list = new ArrayList<>(7);
        if (null == signIn) {
            // 没有签过到
            for (int i = 1; i < 8; i++) {
                list.add(new SignInDto(i, 0));
            }
        } else {
            // 签过到
            Integer continueDays = signIn.getContinueDays();
            // 1, 前六天的 flag是要固定的
            if (continueDays <= 6) {
                for (int i = 1; i < 8; i++) {
                    if (i <= continueDays) {
                        list.add(new SignInDto(i, 1));
                    } else {
                        list.add(new SignInDto(i, 0));
                    }
                }
            } else {
                // 2, 6天后的签到天数要跟随日期增加
                for (int i = 5; i > -2; i--) {
                    if (i > -1) {
                        list.add(new SignInDto(continueDays - i, 1));
                    } else {
                        list.add(new SignInDto(continueDays + 1, 0));
                    }
                }
            }
        }
        return R.ok().data("sigin",list);
    }
}
